package com.nercel.dsj.gksales.util;

import java.text.SimpleDateFormat;
import java.util.Date;

public class CouponUtil {
    final static String CHARS = "ABCDEFGHIJKLMNPQRSTUVWXYZ23456789";
    final static String stringtable = "ABCDEFGHIJKLMNPQRSTUVWXYZ23456789";

        final static String password = "dak3le2";

        public static String genCode(String type, Integer length, String password){
            String result = type;
            for(int i=0; i<length - 1 - type.length(); i++){
                int index = getRandom(0, CHARS.length() - 1);
                System.out.println(CHARS.length() + "," + index);
                result += CHARS.substring(index, index+1);
            }
            result += Md5Util.getMD5String(result + password).substring(0,1).toUpperCase();
            return result;
        }

        public static Boolean verifyCode(String code, String password){
            String last = code.substring(code.length() - 1, code.length());
            String vcode = code.substring(0, code.length() - 1);
            return Md5Util.getMD5String(vcode + password).substring(0,1).toUpperCase().equalsIgnoreCase(last);
        }

        public static int getRandom(int start,int end) {
            int num = (int) (Math.random()*(end-start+1)+start);
            return num;
        }

        public static void main( String[] args ) throws Exception{
            String code = genCode("U", 8, password);
            System.out.println(code);
            System.out.println(code + " , " + verifyCode(code, password));

            String sa = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()) + "" + (int)(Math.random()*(999-100+1)+100);
            System.out.println(sa);
        }

    }